//
//  TravelViewController.m
//  B
//
//  Created by Znt on 15/11/7.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "TravelViewController.h"
#import "TravelCollectionViewCell.h"
#import "Day.h"
#import "Model4.h"
#import "Model2.h"
#import "ZNT_MapViewController.h"
#import "BaseView.h"

@interface TravelViewController ()<UICollectionViewDelegate ,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong)UICollectionView *collectionView;
@property (nonatomic ,strong)NSMutableArray *allDataArray;

@property (nonatomic ,strong)UILabel *dayLabel ;
@property (nonatomic ,strong)UIImageView *imageView ;
@property (nonatomic ,strong)UIImageView *personIcon;

@property (nonatomic ,strong)UILabel *firstDayLabel;
@property (nonatomic ,strong)UILabel *firstLabel;
@property (nonatomic ,strong)UILabel *secondLabel;
@property (nonatomic ,strong)UILabel *thirdLabel;
@property (nonatomic ,strong)UILabel *forthLabel;
@property (nonatomic ,strong)UILabel *fifthLabel;
@property (nonatomic ,strong)UILabel *sixthLabel;




@end

@implementation TravelViewController

- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = YES;
        }
    }
}

//- (void) viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = NO;
//        }
//    }
//    
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setAllDataArray:[NSMutableArray array]];

    UICollectionViewFlowLayout  * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(325 *XWIDTH, 300 *XHIGHT);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375*XWIDTH, 667 *XHIGHT - 64 ) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[TravelCollectionViewCell  class] forCellWithReuseIdentifier:@"Identifier"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.view addSubview:self.collectionView];
    [[self collectionView] registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"aHeardView"];
    [[self collectionView] registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sHeardView"];
    
    [self getData];
    
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    
    [self creatHeaderSubviews];
    
    
}

- (void)creatHeaderSubviews {

    self.dayLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 *XWIDTH, 0, 200 *XWIDTH, 40 *XHIGHT)];
    self.firstDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 *XWIDTH, 340 *XHIGHT, 200 *XHIGHT, 40 *XHIGHT)];
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375 *XWIDTH, 200)];
    [self.imageView setUserInteractionEnabled:YES];
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];
    
    self.personIcon = [[UIImageView alloc]initWithFrame:CGRectMake(150 *XWIDTH, 163 *XHIGHT, 75 *XWIDTH, 75 *XHIGHT)];
    [self.personIcon sd_setImageWithURL:[NSURL URLWithString:_model2.avatar_m] placeholderImage:nil];
    self.personIcon.layer.masksToBounds = YES;
    self.personIcon.layer.cornerRadius = 37;
    
    self.firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(37 *XWIDTH, 250*XHIGHT, 100*XWIDTH, 40 *XHIGHT)];
    self.firstLabel.textAlignment = NSTextAlignmentCenter;
    self.secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(137*XWIDTH, 250*XHIGHT, 100*XWIDTH, 40*XHIGHT)];
    self.secondLabel.textAlignment = NSTextAlignmentCenter;
    self.thirdLabel = [[UILabel alloc]initWithFrame:CGRectMake(237*XWIDTH, 250*XHIGHT, 100*XWIDTH, 40*XHIGHT)];
    self.thirdLabel.textAlignment = NSTextAlignmentCenter;
    self.forthLabel = [[UILabel alloc]initWithFrame:CGRectMake(37*XWIDTH, 300*XHIGHT, 100*XWIDTH, 40*XHIGHT)];
    self.forthLabel.textAlignment = NSTextAlignmentCenter;
    self.fifthLabel = [[UILabel alloc]initWithFrame:CGRectMake(137*XWIDTH, 300*XHIGHT, 100*XWIDTH, 40*XHIGHT)];
    self.fifthLabel.textAlignment = NSTextAlignmentCenter;
    self.sixthLabel = [[UILabel alloc]initWithFrame:CGRectMake(237*XWIDTH, 300*XHIGHT, 100*XWIDTH, 40*XHIGHT)];
    self.sixthLabel.textAlignment = NSTextAlignmentCenter;

}

- (void) clickCategory:(UITapGestureRecognizer *)tap {

    ZNT_MapViewController *mapVC = [[ZNT_MapViewController alloc]init];
    mapVC.allDataArray = self.allDataArray;
    mapVC.urlString = self.UrlStr;
    [self.navigationController pushViewController:mapVC animated:NO];
    
    
    
    
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0 ) {
        
        CGSize size =    CGSizeMake(375 *XWIDTH, 380 *XHIGHT);
        return size;
    } else {
        
        CGSize size =    CGSizeMake(375 *XWIDTH, 40 *XHIGHT);
        return size;
        
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader ) {
        //代表是表头
        //heardView 是表头重用标识符
        //kind 代表区域类型是表头还是底部
        
        
        if ([indexPath section]== 0) {
            
            UICollectionReusableView *aView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"sHeardView" forIndexPath:indexPath];
            
            self.firstDayLabel.text = @"第1天";
            [aView addSubview:self.firstDayLabel];
            [aView addSubview:self.imageView];
            [aView addSubview:self.personIcon];
            [aView addSubview:self.firstLabel];
            [aView addSubview:self.secondLabel];
            [aView addSubview:self.thirdLabel];
            [aView addSubview:self.forthLabel];
            [aView addSubview:self.fifthLabel];
            [aView addSubview:self.sixthLabel];
            return aView;
            
        } else {
            
            UICollectionReusableView *aView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"aHeardView" forIndexPath:indexPath];
            
            self.dayLabel.text = nil;
            self.dayLabel.text = [NSString stringWithFormat:@"第%ld天",indexPath.section + 1];
            
            [aView addSubview:self.dayLabel];
            
            return aView;
            
            
           
            
        }
        
        
        
    }
    return  nil;
}


-(void)leftButtonAction
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.allDataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    Day *day = [self allDataArray][ section ];
    
    return day.waypoints.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    Day *day = [self allDataArray][indexPath.section];
    Model4 *model4 = [day waypoints][indexPath.row];
    CGFloat height = [AppTools heightForCell:model4.text andFont:17];
    CGSize size = CGSizeMake(WIDTH, (height +400) *XHIGHT);
    
    return size;
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    TravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Identifier" forIndexPath:indexPath];
    Day * day = self.allDataArray[indexPath.section];
    Model4 *model = [day waypoints][[indexPath row]];
    cell.model4 = model;
    NSLog(@"%@",NSStringFromCGRect(cell.frame));
    return cell;
}



- (void)getData{

    NSLog(@"-----------%@",self.UrlStr);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:self.UrlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dictionary = responseObject;
        NSArray *array = [dictionary objectForKey:@"days"];
        for (NSDictionary *dic in array) {
            Day *day = [[Day alloc]init];
            [day setValuesForKeysWithDictionary:dic];
            [day initWithModel];
            [[self allDataArray]addObject:day];
        }
        
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:[dictionary objectForKey:@"trackpoints_thumbnail_image"]] placeholderImage:nil];
        
        self.firstLabel.text = [dictionary objectForKey:@"first_day"];
        self.secondLabel.text = @"里程";
        self.thirdLabel.text = @"喜欢";
        self.forthLabel.text = [NSString stringWithFormat:@"%@ days",[dictionary objectForKey:@"day_count"] ];
        
        NSString * a = [dictionary objectForKey:@"mileage"];
        self.fifthLabel.text = [NSString stringWithFormat:@"%ld公里",a.integerValue];
        self.sixthLabel.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"recommendations"] ];

        
        
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        
    }];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
